import 'dart:io';

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';
import 'package:images_picker/images_picker.dart';
import 'package:meal/constant/antd_icons.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';
import 'package:meal/utils/tools.dart';

///
/// 添加团购商品
///
class GroupCommodity extends StatefulWidget {
  const GroupCommodity({Key? key}) : super(key: key);

  @override
  State<GroupCommodity> createState() => _GroupCommodityState();
}

class _GroupCommodityState extends State<GroupCommodity> {
  List<Media> imgs = [];

  List<BrnMultiDataPickerEntity> data = [];

  String unit = '份';

  double get leftWidth {
    return Sizes.default90;
  }

  double get rowHeight {
    return Sizes.default60;
  }

  TextSpan get requiredSpan {
    return TextSpan(text: ' *', style: StylesUtils.customTextStyle(color: ColorsUtils.colorFf0));
  }

  @override
  void initState() {
    super.initState();
    data.add(BrnMultiDataPickerEntity(text: '份', value: 1));
    data.add(BrnMultiDataPickerEntity(text: '杯', value: 2));
    data.add(BrnMultiDataPickerEntity(text: '听', value: 3));
    data.add(BrnMultiDataPickerEntity(text: '打', value: 4));
    data.add(BrnMultiDataPickerEntity(text: '块', value: 5));
    data.add(BrnMultiDataPickerEntity(text: '扎', value: 6));
    data.add(BrnMultiDataPickerEntity(text: '位', value: 7));
    data.add(BrnMultiDataPickerEntity(text: '个', value: 8));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ToolsUtils.getDefaultBackgroundColor(),
      appBar:
          ToolsUtils.getDefaultBrnAppBar('添加商品', actions: ToolsUtils.actions('确定', pressed: () {})),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              margin: const EdgeInsets.only(top: Sizes.default20),
              padding:
                  const EdgeInsets.symmetric(horizontal: Sizes.default15, vertical: Sizes.default8),
              decoration: const BoxDecoration(color: Colors.white),
              child: ListBody(
                children: [
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(
                              TextSpan(
                                  text: '名称',
                                  style: StylesUtils.customTextStyle(
                                      fontSize: Sizes.default15, color: ColorsUtils.color333),
                                  children: [requiredSpan]),
                            )),
                        Expanded(
                            child: Container(
                          height: Sizes.default45,
                          padding: const EdgeInsets.symmetric(horizontal: Sizes.default10),
                          decoration: BoxDecoration(
                              color: ColorsUtils.rgba(247, 247, 247, 1),
                              borderRadius:
                                  const BorderRadius.all(Radius.circular(Sizes.default10))),
                          child: TextField(
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color363, fontSize: Sizes.default15),
                            decoration: InputDecoration(
                                border: const OutlineInputBorder(
                                  borderSide: BorderSide.none,
                                ),
                                isCollapsed: true,
                                filled: true,
                                fillColor: Colors.transparent,
                                contentPadding: const EdgeInsets.symmetric(
                                    horizontal: Sizes.default10, vertical: Sizes.default10),
                                constraints: const BoxConstraints(minHeight: Sizes.default45),
                                hintText: '如柠檬茶',
                                hintStyle: StylesUtils.customTextStyle(
                                    fontSize: Sizes.default14, color: ColorsUtils.color999)),
                            keyboardType: TextInputType.text,
                            onChanged: (String content) {},
                          ),
                        ))
                      ],
                    ),
                  ),
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(TextSpan(children: [
                              TextSpan(
                                text: '商品数量',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                              ),
                            ]))),
                        Expanded(
                            child: Container(
                          height: Sizes.default45,
                          padding: const EdgeInsets.symmetric(horizontal: Sizes.default10),
                          margin: const EdgeInsets.only(right: Sizes.default15),
                          decoration: BoxDecoration(
                              color: ColorsUtils.rgba(247, 247, 247, 1),
                              borderRadius:
                                  const BorderRadius.all(Radius.circular(Sizes.default10))),
                          child: TextField(
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color363, fontSize: Sizes.default15),
                            decoration: InputDecoration(
                                border: const OutlineInputBorder(
                                  borderSide: BorderSide.none,
                                ),
                                isCollapsed: true,
                                filled: true,
                                fillColor: Colors.transparent,
                                contentPadding: const EdgeInsets.symmetric(
                                    horizontal: Sizes.default10, vertical: Sizes.default10),
                                constraints: const BoxConstraints(minHeight: Sizes.default45),
                                hintText: '例如900',
                                hintStyle: StylesUtils.customTextStyle(
                                    fontSize: Sizes.default14, color: ColorsUtils.color999)),
                            keyboardType: TextInputType.number,
                            onChanged: (String content) {},
                          ),
                        )),
                        InkWell(
                          onTap: () {
                            ToolsUtils.openDataPicker(context, data, (List selectedIndexList) {
                              setState(() {
                                int index = selectedIndexList[0];
                                unit = data[index].text;
                              });
                            });
                          },
                          child: Container(
                              alignment: Alignment.centerRight,
                              child: Text.rich(
                                TextSpan(
                                    text: unit,
                                    style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                                    children: [
                                      WidgetSpan(
                                          child: BrunoTools.getAssetSizeImage(
                                              BrnAsset.iconDownArrow, 15, 15))
                                    ]),
                              )),
                        )
                      ],
                    ),
                  ),
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(TextSpan(children: [
                              TextSpan(
                                text: '商品原价',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                              ),
                            ]))),
                        Expanded(
                            child: Container(
                          height: Sizes.default45,
                          padding: const EdgeInsets.symmetric(horizontal: Sizes.default10),
                          margin: const EdgeInsets.only(right: Sizes.default15),
                          decoration: BoxDecoration(
                              color: ColorsUtils.rgba(247, 247, 247, 1),
                              borderRadius:
                                  const BorderRadius.all(Radius.circular(Sizes.default10))),
                          child: TextField(
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color363, fontSize: Sizes.default15),
                            decoration: InputDecoration(
                                border: const OutlineInputBorder(
                                  borderSide: BorderSide.none,
                                ),
                                isCollapsed: true,
                                filled: true,
                                fillColor: Colors.transparent,
                                contentPadding: const EdgeInsets.symmetric(
                                    horizontal: Sizes.default10, vertical: Sizes.default10),
                                constraints: const BoxConstraints(minHeight: Sizes.default45),
                                hintText: '例如900',
                                hintStyle: StylesUtils.customTextStyle(
                                    fontSize: Sizes.default14, color: ColorsUtils.color999)),
                            keyboardType: TextInputType.number,
                            onChanged: (String content) {},
                          ),
                        )),
                        Container(
                          alignment: Alignment.centerRight,
                          child: Text('￥',
                              style: StylesUtils.customTextStyle(color: ColorsUtils.color333)),
                        )
                      ],
                    ),
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        width: leftWidth,
                        alignment: Alignment.topLeft,
                        child: Text.rich(TextSpan(
                            text: '商品图片',
                            style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                            children: [requiredSpan])),
                      ),
                      Expanded(
                          child: Container(
                        alignment: Alignment.centerLeft,
                        child: Wrap(
                          spacing: Sizes.default10,
                          runSpacing: Sizes.default10,
                          children: [
                            ...imgs.map((e) => Stack(
                                  children: [
                                    GFImageOverlay(
                                      image: FileImage(File(e.path)),
                                      width: 105,
                                      height: 105,
                                      borderRadius: BorderRadius.circular(Sizes.default5),
                                    ),
                                    Positioned(
                                        top: 10,
                                        right: 10,
                                        child: IconButton(
                                          onPressed: () {
                                            setState(() {
                                              imgs.remove(e);
                                            });
                                          },
                                          icon: Icon(AntdIcons.delete,
                                              size: Sizes.default20,
                                              color: ColorsUtils.parseColorFromHexString(
                                                  ColorsUtils.colorFf0)),
                                        ))
                                  ],
                                )),
                            InkWell(
                              onTap: () {
                                ToolsUtils.openCupertinoModalPopup(
                                    context, setMediaImg, setMediaImg);
                              },
                              child: Container(
                                width: 105,
                                height: 105,
                                decoration: BoxDecoration(
                                    color: ColorsUtils.rgba(247, 247, 247, 1),
                                    borderRadius: BorderRadius.circular(Sizes.default5)),
                                alignment: Alignment.center,
                                child: ToolsUtils.getUrlSvg(
                                    'https://cdn7.axureshop.com/demo/1525603/images/发布商品_单品/u1046.svg',
                                    width: 24,
                                    height: 20),
                              ),
                            )
                          ],
                        ),
                      ))
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  void setMediaImg(List<Media> res) {
    setState(() {
      imgs.addAll(res);
    });
  }
}
